<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\System as SystemModel;
use think\Cache;
use think\Db;

/**
 * 系统配置
 * Class System
 * @package app\admin\controller
 */
class System extends AdminBase
{
    protected $system_model;

    public function _initialize()
    {
        parent::_initialize();
        $this->system_model        = new SystemModel();
    }

    /**
     * 站点配置
     */
    public function index(){
        $info = Db::name('system')->select();

        foreach ($info as $k => $v) {
            $info[$k]['config'] = json_decode($info[$k]['config'],true);
        }
// halt($info);
        $this->assign('info',$info);

        return $this->fetch('index');
    }

    /**
     * 更新配置
     */
    public function edit_proc(){
        $data  = $this->request->param('');
        $code = $this->request->param('code','','trim');
        $data['update_time'] = time();

        //根据传入的code生成不同的验证场景
        $validate_result = $this->validate($data, 'System.'.$code);

        if ($validate_result !== true) {
                $this->error($validate_result);
        }

        // 处理数据
        switch ($code) {
            case 'basics':
                $config = array('prompt'=>$data['prompt'],'record'=>$data['record'],'email'=>$data['email'],'remark'=>$data['remark']);
                $data['config'] = json_encode($config);
                break;
            case 'seo':
                $config = array('title'=>$data['title'],'keyword'=>$data['keyword'],'author'=>$data['author'],'describe'=>$data['describe']);
                $data['config'] = json_encode($config);
                break;
            case 'wxpay':
                $config = array('appid'=>$data['appid'],'appsecret'=>$data['appsecret'],'mchid'=>$data['mchid'],'mchkey'=>$data['mchkey']);
                $data['config'] = json_encode($config);
                break;
            case 'alipay':
                $config = array('aliaccounts'=>$data['aliaccounts'],'alipid'=>$data['alipid'],'alikey'=>$data['alikey']);
                $data['config'] = json_encode($config);
                break;
            case 'email':
                $config = array('smtpserver'=>$data['smtpserver'],'sendemail'=>$data['sendemail'],'emailpassword'=>$data['emailpassword'],'addresser'=>$data['addresser']);
                $data['config'] = json_encode($config);
                break;
            default:
                $this->error('非法操作！');
                break;
        }

// halt($data);

        // allowField表示过滤非数据表中字段的数据
        $res = $this->system_model->allowField(true)->save($data,$data['id']);

        if ($res === false) {
            // 记录日志
            // $logData = array('module' => '规则管理' , 'action' =>  '添加规则' , 'remark' => '添加规则失败' , 'status' => 0);
            // recordActionLog($logData);
            $this->error($this->system_model->getError());
        } else {
            // 记录日志
            // $logData = array('module' => '规则管理' , 'action' => '添加规则' , 'remark' => '分类编号：' . $id , 'status' => 1);
            // recordActionLog($logData);
            $this->success('保存成功');
        }


    }


    /**
     * 清除缓存
     */
    public function clear()
    {
        if (delete_dir_file(CACHE_PATH) || delete_dir_file(TEMP_PATH)) {
            $this->success('清除缓存成功');
        } else {
            $this->error('清除缓存失败');
        }
    }
}